﻿<?php
######################################################
#		Администраторски дел за кадар				 #
######################################################

//Има пристап корисник со администраторски привилегии

require_once APPPATH.'presenters/summary_presenter.php';
require_once 'application/controllers/admin.php';

class adminStaff extends Admin{

	public function __construct(){
		parent::__construct();
	}

	public function index(){
		$user = $this->session->userdata('user');
		if($this->userCheck('admin', 'admin'))
			$this->allStaff();
		else
			$this->staff($user->staffProfile);
	}

	//Функција која прикажува копче за додавање нов кадар
	//и табела за менување на постоечкиот кадар или бришење на кадар
	public function allStaff(){
		$user = $this->userCheck('admin','admin/main');

		$this->load->model('summary_model');
		$this->load->helper('form');
		$data['title'] = 'ФИНКИ - Администратор - Кадар';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStaff/allStaff';
		$data['navbar'] = 'admin/main';
		$data['role'] = $user->role;
		$data['staff'] = $this->summary_model->getWhere(array('language'=>'mk'));

		$this->load->view('masterPage', $data);

	}

	//Функција за приказ на профил на вработен
	public function staff($id){
		$user = $this->userCheck('staff','admin/main');

		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('summary_model');

		$data['title'] = 'ФИНКИ - Администратор - Профил';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStaff/staff';
		$data['navbar'] = 'admin/main';
		$data['role'] = $user->role;
		$data['staff'] = new summary_presenter($this->summary_model->getBy(array('idStaffFk'=>$id,'language'=>'mk')),'mk');
		$data['id'] = $id;

		$this->load->view('masterPage', $data);
	}
	
	//Помошна функција за добивање на титули на кадарот
	private function getLangArray($lang)
	{
		if($lang == "mk"){
			return array("Редовен професор","Вонреден професор","Насловен вонреден професор","Доцент", "Насловен доцент", "Асистент","Помлад асистент","Соработник","Демонстратор","Референт во студентска служба","Систем инженер","Технички секретар","Секретар", "Референт во кадрова служба","Декан","Продекан за финансии","Продекан за наука и соработка","Продекан за настава");
		}
		else{
			return array("Professor","Associate professor","Elected associate professor","Assistant professor", "Elected assistant professor", " Assistant","Junior Assistant","Associate","Demonstrator","Student affairs officer","System engineer","Technical secretary","Legal affairs officer", "Human resources officer","Dean","Vice Dean for finances","Vice Dean for science and cooperation","Vice Dean for academic affairs");
		}
	}

	//Функција за внесување на информации за кадар
	public function addStaff($lang){
		$user = $this->userCheck('admin','admin/main');

		$this->load->helper('form');

		$data['title'] = 'ФИНКИ - Администратор - Додади кадар';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStaff/addStaff';
		$data['navbar'] = 'admin/main';
		$data['role'] = $user->role;
		$data['lang'] = $lang;
		$data['staff'] = $this->getLangArray($lang);

		$this->load->view('masterPage', $data);
	}

	//Функција за додавање на кадар во база
	public function processStaff(){		
		$user = $this->userCheck('admin','admin/main');

		if(!$this->input->post('submit'))
			redirect('admin/main','refresh');

		$this->load->library('form_validation');
		$this->load->model('staff_model');
		$this->load->model('summary_model');
		$this->load->model('upload_model');

		$this->form_validation->set_rules('nameMk', 'име', 'required|xss_clean');
		$this->form_validation->set_rules('nameEn', 'name', 'required|xss_clean|alpha');
		$this->form_validation->set_rules('surnameMk', 'презиме', 'required|xss_clean');
		$this->form_validation->set_rules('surnameEn', 'surname', 'required|xss_clean');
		$this->form_validation->set_rules('titleMk2', 'титула', 'xss_clean');
		$this->form_validation->set_rules('titleEn2', 'title', 'xss_clean');
		$this->form_validation->set_rules('email', 'e-mail', 'xss_clean|valid_email');


		if ($this->form_validation->run() == FALSE)
		{
			redirect('error','refresh');
		}

		$nameMk = $this->input->post('nameMk', TRUE);
		$nameEn = $this->input->post('nameEn', TRUE);
		$surnameMk = $this->input->post('surnameMk', TRUE);
		$surnameEn = $this->input->post('surnameEn', TRUE);
		$titleMk = $this->input->post('titleMk', TRUE);
		$titleEn = $this->input->post('titleEn', TRUE);
		$titleMk2 = $this->input->post('titleMk2', TRUE);
		$titleEn2 = $this->input->post('titleEn2', TRUE);
		$email = $this->input->post('email', TRUE);
		$position = $this->input->post('position', TRUE);
		$dean = $this->input->post('dean', TRUE)?:NULL;

		$imagePath = $this->upload_model->upload_image('staff',uniqid(),250,300);
		
		$dataStaff = array( 'email'=>$email,
				'position'=>$position,
				'dean'=>$dean,
				'image'=>$imagePath
		);

		$id = $this->staff_model->insert($dataStaff);

		$dataSummaryMk = array( 'name' => $nameMk,
				'surname' => $surnameMk,
				'title' => $titleMk2." ".$titleMk,
				'language' => 'mk',
				'idStaffFk' => $id
		);

		$this->summary_model->insert($dataSummaryMk);

		$dataSummaryEn = array( 'name' => $nameEn,
				'surname' => $surnameEn,
				'title' => $titleEn2." ".$titleEn,
				'language' => 'en',
				'idStaffFk' => $id
		);

		$this->summary_model->insert($dataSummaryEn);

		redirect('adminStaff/allStaff','refresh');
	}

	//Функција за менување на кадар, прима аргументи за id на профилот и за јазикот на содржината
	public function editStaff($id,$lang){
		$user = $this->userCheck('staff','admin/main');

		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');

		$this->load->model('staff_model');
		$this->load->model('summary_model');

		$this->load->helper('form');

		$data['title'] = 'ФИНКИ - Администратор - Промени кадар';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStaff/editStaff';
		$data['navbar'] = 'admin/main';
		$data['role'] = $user->role;
		$data['id'] = $id;
		$data['lang'] = $lang;
		$data['positions'] = $this->getLangArray($lang);
		$data['staff'] = $this->staff_model->getBy(array('idStaffPk'=>$id));
		$data['summ'] = $this->summary_model->getBy(array('language'=>$lang,'idStaffFk'=>$id));

		$this->load->view('masterPage', $data);

	}

	//Функција за промена на податоците во база за одреден вработен
	public function updateStaff($id,$lang){
		$user = $this->userCheck('admin','admin/main');

		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		if(!$this->input->post('submit'))
			redirect('admin/main','refresh');

		$this->load->library('form_validation');
		$this->load->model('staff_model');
		$this->load->model('summary_model');
		$this->load->model('upload_model');

		$this->form_validation->set_rules('name', 'име', 'required|xss_clean');
		$this->form_validation->set_rules('surname', 'презиме', 'required|xss_clean');
		$this->form_validation->set_rules('email', 'e-mail', 'xss_clean|valid_email');
		$this->form_validation->set_rules('phone', 'телефон', 'xss_clean|numeric');

		$name = $this->input->post('name', TRUE);
		$surname = $this->input->post('surname', TRUE);
		$title = $this->input->post('title', TRUE);
		$title2 = $this->input->post('title2', TRUE);
		$phone = $this->input->post('phone', TRUE);
		$email = $this->input->post('email', TRUE);
		$position = $this->input->post('position', TRUE);
		$dean = $this->input->post('dean', TRUE)?:NULL;
		$officeHours = $this->input->post('officeHours', TRUE);
		$summary = $this->input->post('summary', TRUE);
		
		$imagePath = $this->upload_model->upload_image('staff',$surname."_".$name,250,300);

		if ($this->form_validation->run() == FALSE)
		{
			redirect('error','refresh');
		}

		$dataStaff = array('phone' => $phone, 
				'email'=>$email,
				'position'=>$position,
				'dean'=>$dean,
				'officeHours' => $officeHours
		);
		
		if ($imagePath !== FALSE)
			$dataStaff['image'] = $imagePath;

		$this->staff_model->update(array('idStaffPk' => $id), $dataStaff);

		$dataSummary = array( 'name' => $name,
				'surname' => $surname,
				'title' => $title2." ".$title,
				'summary' => $summary,
				'language' => $lang,
				'idStaffFk' => $id
		);

		$this->summary_model->update(array('idStaffFk' => $id, 'language' => $lang), $dataSummary);

		redirect('adminStaff/staff/'.$id,'refresh');
	}

	//Функција за бришење на кадар.
	//Ги брише и соодветните книги, трудови, проекти, курсеви и асоцијации
	private function delete($id){
		$user = $this->userCheck('admin','admin/main');

		$this->load->model('staff_model');
		$this->load->model('books_model');
		$this->load->model('projects_model');
		$this->load->model('papers_model');
		$this->load->model('associations_model');
		$this->load->model('summary_model');

		$this->staff_model->delete(array('idStaffPk'=>$id));
		$this->books_model->delete(array('idStaffFk'=>$id));
		$this->projects_model->delete(array('idStaffFk'=>$id));
		$this->papers_model->delete(array('idStaffFk'=>$id));
		$this->associations_model->delete(array('idStaffFk'=>$id));
		$this->summary_model->delete(array('idStaffFk'=>$id));

		redirect('adminStaff/allStaff','refresh');
	}
	
	//Функција за бришење на повеќе вработени.
	public function deleteStaff(){
		$user = $this->userCheck('staff','admin/main');
		
		
		$this->load->model('staff_model');
		
		$staffs = $this->staff_model->getAll();
		
		foreach($staffs as $staff)
			if($this->input->post($staff->idStaffPk)!=null)
			$this->delete($staff->idStaffPk);
		
		redirect('adminStaff/staff','refresh');
	}

	######################################################
	#		Администраторски дел за кадар - книги		 #
	######################################################
	
	//Функција за приказ на сите книги од одреден вработен
	public function books($id){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('books_model');
		$this->load->helper('form');
		
		$data['title'] = 'ФИНКИ - Администратор - Книги';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStaff/books';
		$data['navbar'] = 'admin/main';
		$data['role'] = $user->role;
		$data['books'] = $this->books_model->getWhere(array('idStaffFk'=>$id));
		$data['id'] = $id;
		
		$this->load->view('masterPage', $data);
	}
	
	//Функција за додавање на книга кај одреден вработен
	public function addBook($id){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('books_model');
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('title', 'наслов', 'required|xss_clean');
		$this->form_validation->set_rules('publisher', 'издавач', 'required|xss_clean');
		
		if ($this->form_validation->run() == FALSE)
		{
			redirect('error','refresh');
		}
		
		$data['title'] = $this->input->post('title', TRUE);
		$data['publisher'] = $this->input->post('publisher', TRUE);
		$data['idStaffFk'] = $id;
		
		$this->books_model->insert($data);
		
		redirect('adminStaff/books/'.$id);
	}
	
	//Функција за промена на информации за книга
	public function editBook($id,$staffId){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('books_model');
		$this->load->helper('form');
		
		$data['title'] = 'ФИНКИ - Администратор - Книги';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStaff/editBook';
		$data['navbar'] = 'admin/main';
		$data['role'] = $user->role;
		$data['books'] = $this->books_model->getWhere(array('idStaffFk'=>$id));
		$data['theBook'] = $this->books_model->getBy(array('idBksPk'=>$id));
		$data['id'] = $staffId;
		
		$this->load->view('masterPage', $data);
	}
	
	//Функција за запишување на променетите информации за одредена книга
	//во база
	public function updateBook($id,$idStaff){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('books_model');
		$this->load->library('form_validation');
		
		if(!$this->input->post('submit'))
			redirect('adminStaff/books/'.$staffId);
		
		$this->form_validation->set_rules('title', 'наслов', 'required|xss_clean');
		$this->form_validation->set_rules('publisher', 'издавач', 'required|xss_clean');
		
		if ($this->form_validation->run() == FALSE)
		{
			redirect('error','refresh');
		}
		
		$data['title'] = $this->input->post('title', TRUE);
		$data['publisher'] = $this->input->post('publisher', TRUE);
		
		$this->books_model->update(array('idBksPk'=>$id),$data);
		
		redirect('adminStaff/books/'.$idStaff);
	}
	
	//Функција за бришење на книги кај одреден корисник
	public function deleteBooks($staffId){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('books_model');
		
		$books = $this->books_model->getWhere(array('idStaffFk'=>$staffId));
		
		foreach($books as $book)
			if($this->input->post($book->idBksPk)!=null)
				$this->books_model->delete(array('idBksPk'=>$book->idBksPk));

		redirect('adminStaff/books/'.$staffId,'refresh');
	}
	
	######################################################
	#		Администраторски дел за кадар - проекти		 #
	######################################################
	
	//Функција за приказ на сите проекти од одреден вработен
	public function projects($id){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('projects_model');
		$this->load->helper('form');
		
		$data['title'] = 'ФИНКИ - Администратор - Проекти';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStaff/projects';
		$data['navbar'] = 'admin/main';
		$data['role'] = $user->role;
		$data['projects'] = $this->projects_model->getWhere(array('idStaffFk'=>$id));
		$data['id'] = $id;
		
		$this->load->view('masterPage', $data);
	}

	//Функција за додавање на проект кај одреден вработен
	public function addProject($id){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('projects_model');
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('name', 'име', 'required|xss_clean');
		$this->form_validation->set_rules('sponsor', 'спонзор', 'required|xss_clean');
		
		if ($this->form_validation->run() == FALSE)
		{
			redirect('error','refresh');
		}
		
		$data['name'] = $this->input->post('name', TRUE);
		$data['sponsor'] = $this->input->post('sponsor', TRUE);
		$data['idStaffFk'] = $id;
		
		$this->projects_model->insert($data);
		
		redirect('adminStaff/projects/'.$id);
	}

	//Функција за промена на информации за проект
	public function editProject($id,$staffId){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('projects_model');
		$this->load->helper('form');
		
		$data['title'] = 'ФИНКИ - Администратор - Проекти';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStaff/editProject';
		$data['navbar'] = 'admin/main';
		$data['role'] = $user->role;
		$data['projects'] = $this->projects_model->getWhere(array('idStaffFk'=>$id));
		$data['theProject'] = $this->projects_model->getBy(array('idPrjtPk'=>$id));
		$data['id'] = $staffId;
		
		$this->load->view('masterPage', $data);
	}
	
	//Функција за запишување на променетите информации за одреден проект
	//во база
	public function updateProject($id,$idStaff){
		$user = $this->userCheck('staff','admin/main');
	
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
	
		$this->load->model('projects_model');
		$this->load->library('form_validation');
	
		if(!$this->input->post('submit'))
			redirect('adminStaff/projects/'.$staffId);
	
		$this->form_validation->set_rules('name', 'име', 'required|xss_clean');
		$this->form_validation->set_rules('sponsor', 'спонзор', 'required|xss_clean');
	
		if ($this->form_validation->run() == FALSE)
		{
			redirect('error','refresh');
		}
		
		$data['name'] = $this->input->post('name', TRUE);
		$data['sponsor'] = $this->input->post('sponsor', TRUE);
	
		$this->projects_model->update(array('idPrjtPk'=>$id),$data);
	
		redirect('adminStaff/projects/'.$idStaff);
	}

	//Функција за бришење на проекти кај одреден корисник
	public function deleteProjects($staffId){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('projects_model');
		
		$projects = $this->projects_model->getWhere(array('idStaffFk'=>$staffId));
		
		foreach($projects as $project)
			if($this->input->post($project->idPrjtPk)!=null)
			$this->projects_model->delete(array('idPrjtPk'=>$project->idPrjtPk));
		
		redirect('adminStaff/projects/'.$staffId,'refresh');
	}

	######################################################
	#		Администраторски дел за кадар - трудови		 #
	######################################################
	
	//Функција за приказ на сите трудови од одреден вработен
	public function papers($id){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('papers_model');
		$this->load->helper('form');
		
		$data['title'] = 'ФИНКИ - Администратор - Трудови';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStaff/papers';
		$data['navbar'] = 'admin/main';
		$data['role'] = $user->role;
		$data['papers'] = $this->papers_model->getWhere(array('idStaffFk'=>$id));
		$data['id'] = $id;
		
		$this->load->view('masterPage', $data);
	}

	//Функција за додавање на труд кај одреден вработен
	public function addPaper($id){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('papers_model');
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('title', 'наслов', 'required|xss_clean');
		$this->form_validation->set_rules('place', 'место', 'required|xss_clean|alpha_dash');
		$this->form_validation->set_rules('country', 'држава', 'xss_clean|alpha');
		
		if ($this->form_validation->run() == FALSE)
		{
			redirect('error','refresh');
		}
		
		$data['title'] = $this->input->post('title', TRUE);
		$data['place'] = $this->input->post('place', TRUE);
		$data['country'] = $this->input->post('country', TRUE);
		$data['idStaffFk'] = $id;
		
		$this->papers_model->insert($data);
		
		redirect('adminStaff/papers/'.$id);
	}

	//Функција за промена на информации за труд
	public function editPaper($id,$staffId){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('papers_model');
		$this->load->helper('form');
		
		$data['title'] = 'ФИНКИ - Администратор - Трудови';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStaff/editPaper';
		$data['navbar'] = 'admin/main';
		$data['role'] = $user->role;
		$data['papers'] = $this->papers_model->getWhere(array('idStaffFk'=>$id));
		$data['thePaper'] = $this->papers_model->getBy(array('idPapPk'=>$id));
		$data['id'] = $staffId;
		
		$this->load->view('masterPage', $data);
	}
	
	//Функција за запишување на променетите информации за одреден труд
	//во база
	public function updatePaper($id,$idStaff){
		$user = $this->userCheck('staff','admin/main');
	
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
	
		$this->load->model('papers_model');
		$this->load->library('form_validation');
	
		if(!$this->input->post('submit'))
			redirect('adminStaff/projects/'.$staffId);
	
		$this->form_validation->set_rules('title', 'наслов', 'required|xss_clean');
		$this->form_validation->set_rules('place', 'место', 'required|xss_clean|alpha_dash');
		$this->form_validation->set_rules('country', 'држава', 'required|xss_clean|alpha');
	
		if ($this->form_validation->run() == FALSE)
		{
			redirect('error','refresh');
		}
		
		$data['title'] = $this->input->post('title', TRUE);
		$data['place'] = $this->input->post('place', TRUE);
		$data['country'] = $this->input->post('country', TRUE);
	
		$this->papers_model->update(array('idPapPk'=>$id),$data);
	
		redirect('adminStaff/papers/'.$idStaff);
	}

	//Функција за бришење на трудови кај одреден корисник
	public function deletePapers($staffId){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('papers_model');
		
		$projects = $this->papers_model->getWhere(array('idStaffFk'=>$staffId));
		
		foreach($projects as $project)
			if($this->input->post($project->idPapPk)!=null)
			$this->papers_model->delete(array('idPapPk'=>$project->idPapPk));
		
		redirect('adminStaff/papers/'.$staffId,'refresh');
	}

	######################################################
	#	 Администраторски дел за кадар - здруженија		 #
	######################################################
	
	//Функција за приказ на сите здруженија кај одреден вработен
	public function associations($id){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('associations_model');
		$this->load->helper('form');
		
		$data['title'] = 'ФИНКИ - Администратор - Здруженија';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStaff/associations';
		$data['navbar'] = 'admin/main';
		$data['role'] = $user->role;
		$data['assocs'] = $this->associations_model->getWhere(array('idStaffFk'=>$id));
		$data['id'] = $id;
		
		$this->load->view('masterPage', $data);
	}

	//Функција за додавање на здружение кај одреден вработен
	public function addAssociation($id){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('associations_model');
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('name', 'име', 'required|xss_clean');
		$this->form_validation->set_rules('link', 'link', 'xss_clean|prep_url');
		
		if ($this->form_validation->run() == FALSE)
		{
			redirect('error','refresh');
		}
		
		$data['name'] = $this->input->post('name', TRUE);
		$data['link'] = $this->input->post('link', TRUE);
		$data['idStaffFk'] = $id;
		
		$this->associations_model->insert($data);
		
		redirect('adminStaff/associations/'.$id);
	}
	
	//Функција за промена на информации за здружение
	public function editAssociation($id,$staffId){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('associations_model');
		$this->load->helper('form');
		
		$data['title'] = 'ФИНКИ - Администратор - Здруженија';
		$data['include']="admin/adminContent";
		$data['content'] = 'adminStaff/editAssociation';
		$data['navbar'] = 'admin/main';
		$data['role'] = $user->role;
		$data['assocs'] = $this->associations_model->getWhere(array('idStaffFk'=>$id));
		$data['theAssoc'] = $this->associations_model->getBy(array('idAscPk'=>$id));
		$data['id'] = $staffId;
		
		$this->load->view('masterPage', $data);
	}
	
	//Функција за запишување на променетите информации за одредено здружение
	//во база
	public function updateAssociation($id,$idStaff){
		$user = $this->userCheck('staff','admin/main');
	
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
	
		$this->load->model('associations_model');
		$this->load->library('form_validation');
	
		if(!$this->input->post('submit'))
			redirect('adminStaff/associations/'.$staffId);
	
		$this->form_validation->set_rules('name', 'наслов', 'required|xss_clean');
		$this->form_validation->set_rules('link', 'место', 'xss_clean|prep_url');
	
		if ($this->form_validation->run() == FALSE)
		{
			redirect('error','refresh');
		}
		
		$data['name'] = $this->input->post('name', TRUE);
		$data['link'] = $this->input->post('link', TRUE);
	
		$this->associations_model->update(array('idAscPk'=>$id),$data);
	
		redirect('adminStaff/associations/'.$idStaff);
	}

	//Функција за бришење на здруженија кај одреден корисник
	public function deleteAssociations($staffId){
		$user = $this->userCheck('staff','admin/main');
		
		if($user->role != 1 && $user->staffProfile != $id)
			redirect('adminStaff/staff/'.$user->staffProfile,'refresh');
		
		$this->load->model('associations_model');
		
		$assocs = $this->associations_model->getWhere(array('idStaffFk'=>$staffId));
		
		foreach($assocs as $assoc)
			if($this->input->post($assoc->idPapPk)!=null)
			$this->associations_model->delete(array('idAscPk'=>$assoc->idAscPk));
		
		redirect('adminStaff/associations/'.$staffId,'refresh');
	}

}
?>